//TODO: License Header.

/*
This code is licensed under the BSD license.

*/
package analysis.metrics

import primitives.graph.Node
import search.util.TreeLoader2
import static Math.min
import primitives.cluster.*;
import primitives.graph.*;

class HCC extends Metric{

	def static outEdges(IClusterLevel c){
        return edgesFilteredBy(c,{
                trans -> 
                !c.nodes.contains(trans.destinationNode)
          
        });
    }

	def static intraEdges(IClusterLevel c){
		return edgesFilteredBy(c,{
                trans -> 
                c.nodes.contains(trans.destinationNode)
          
        });
	}

	def static edgesFilteredBy(IClusterLevel c, clj){
		Collection<Transition> nodes = []
        c.nodes.each{
            node ->
            nodes.addAll(node.getTransitionsAsT().findAll(clj))
        }
        return nodes
	}
	
	static double evaluate(IClusterLevel tree, int depth){
		int t = 0;
		if(tree.encapsulates()){
			t = tree.children.collect{
			
			    	evaluate(it,depth+1)
        			}.sum(0)
		}else{
			return 0;
		}
		
		int n;		int e;
		if(tree.encapsulates()){
		 		n = tree.children.size()
				e = tree.children.collect{
		    			outEdges(it).size()
        				}.sum(0)
		}
		
		
		return ((e - n + 2) / depth) + t
	}
	
	static double evaluate(IClusterLevel tree){
		

		
		int t = tree.children.collect{
		    evaluate(it,1)
        	}.sum(0)
        
		return t;
		

        
	}

	double evaluate(ClusterHead tree){
		return evaluate((IClusterLevel)tree);
	}

	public static void main(String[] args){
		new HCC().proc(args);
	}
}